
import requests
import  re
import os

if __name__ == "__main__":
    #创建文件夹保存图片
    if not os.path.exists('./qiutuLibs'):
        os.mkdir('./qiutuLibs')


    url = 'https://www.qiushibaike.com/imgrank/'
    headers = {
            'user-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
        }

    #通用爬虫对整页爬取
    page_text = requests.get(url=url,headers=headers).text

    #使用聚焦爬虫将页面中所有的图片进行提取

    ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
    img_src_list = re.findall(ex,page_text,re.S)
    #print(img_src_list)
    for src in img_src_list:
        #拼接完整url
        src = 'https:'+src
        img_data = requests.get(url=src,headers=headers).content
        #存储不同的名字
        img_name = src.split('/')[-1]
        #存储路径
        imgPath = './qiutuLibs/'+img_name
        #储存命令
        with open(imgPath,'wb') as fp:
            fp.write(img_data)
            print(img_name,'下载成功！！')